using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows.Forms;
using Office = Microsoft.Office.Core;
using Outlook = Microsoft.Office.Interop.Outlook;
using MAPIConcubine;

namespace SetAppointmentLabelColor
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            for (int i = 0; i <= 10; i++)
            {
                comboBox1.Items.Add((Fabric.AppointmentLabelColor) i); 

            }
            comboBox1.SelectedIndex = 0;

        }

        private void button1_Click(object sender, EventArgs e)
        {
            // Create an Outlook session
            object missing = System.Reflection.Missing.Value;


            // The selected Item
            Outlook.AppointmentItem selectedItem = null;

            // get the Outlook Application Object
            Outlook.Application outlookApplication = new Outlook.Application();

            // get the namespace object
            Outlook.NameSpace olNamespace = outlookApplication.GetNamespace("MAPI");

            // Logon to Session, here we use an already opened Outlook
            olNamespace.Logon(missing, missing, false, false);


            // Use the fabric to get the current profilename
            Fabric fabric = new Fabric();
            try
            {
                // check if we have a selected appointment
                selectedItem = outlookApplication.ActiveInspector().CurrentItem  as Outlook.AppointmentItem ;

                // Apply the selected color from the combobox...
                fabric.SetAppointmentLabelColor(selectedItem.MAPIOBJECT, (Fabric.AppointmentLabelColor) comboBox1.SelectedItem );

            }
            catch (System.Exception ex)
            {
                MessageBox.Show(this,  ex.Message);
            }
            finally
            {
                selectedItem = null;
                fabric = null;
                olNamespace.Logoff();
                olNamespace = null;
                outlookApplication = null;

                GC.Collect();
                GC.WaitForPendingFinalizers();
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            // Create an Outlook session
            object missing = System.Reflection.Missing.Value;

            // The selected Item
            Outlook.AppointmentItem selectedItem = null;

            // get the Outlook Application Object
            Outlook.Application outlookApplication = new Outlook.Application();

            // get the namespace object
            Outlook.NameSpace olNamespace = outlookApplication.GetNamespace("MAPI");

            // Logon to Session, here we use an already opened Outlook
            olNamespace.Logon(missing, missing, false, false);

            // Use the fabric to get the current profilename
            Fabric fabric = new Fabric();
            try
            {
                // check if we have a selected appointment
                selectedItem = outlookApplication.ActiveInspector().CurrentItem as Outlook.AppointmentItem;

                // get the current LabelColor and display it in  a MB
                Fabric.AppointmentLabelColor current = fabric.GetAppointmentLabelColor(selectedItem.MAPIOBJECT);

                MessageBox.Show (this, String.Format ("The current AppointmentLabel Color is : {0}", current ));
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(this,  ex.Message);
            }
            finally
            {
                selectedItem = null;
                fabric = null;
                olNamespace.Logoff();
                olNamespace = null;
                outlookApplication = null;

                GC.Collect();
                GC.WaitForPendingFinalizers();
            }
        }
    }
}